﻿//-----------------------------------------------------------------------------
//<filedescription file="IntegrationKit.Utilities.Validation.cs" company="Microsoft">
//  <copyright>
//     Copyright © Microsoft Corporation.  All rights reserved.
//     ***  DESIGNATED SAMPLE CODE (per the included software license terms) ***
//      THIS CODE SAMPLE IS MADE AVAILABLE TO YOU WITHOUT WARRANTY OF ANY KIND
//      AND CONFERS NO RIGHTS ON YOU WHATSOEVER EXCEPT THE RIGHT TO REVIEW IT
//      SOLELY AS A REFERENCE.  THE ENTIRE RISK OF USE OR RESULTS FROM USE OF
//      THIS CODE SAMPLE REMAINS WITH YOU.
//  </copyright>
//  <purpose>
//     Provides utility methods specific to validation of string data via RegEx
//  </purpose>
//  <notes>
//  </notes>
//</filedescription>
//-----------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Security;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;
using System.Xml.XPath;
using Microsoft.Win32;
using Microsoft.ConfigurationManagement.AdminConsole;


namespace Microsoft.ConfigurationManagement.IntegrationDevelopmentKit
{
    /// <summary>
    /// 
    /// </summary>
    public partial class IntegrationKitUtilities
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param taskName="fileName"></param>
        /// <returns></returns>
        public static bool IsValidFileName(string fileName)
        {
            // LogLine(LogFile, "IsValidFileName:\tStarting..."); 
            Regex reg = new Regex(RegExLibrary.GetRegularExpression(RegExExpression.FileName));
            return reg.IsMatch(fileName);
            //Regex validFileNameRegEx = new Regex("^[^/\\\\:*?\"<>|\\r\\n]{1,255}$");
            //return validFileNameRegEx.IsMatch(fileName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param taskName="path"></param>
        /// <returns></returns>
        public static bool IsValidNetworkPath(string path)
        {
            // LogLine(LogFile, "IsValidNetworkPath:\tStarting...");
            Regex validNetPathRegEx = new Regex(@"^(?=^.{5,503}$)\\\\[^\\/""\[\]; |=,+*?<>]{1,255}(\\[^\\/:*?""<>|]{1,244})+[\\]?$");
            return validNetPathRegEx.IsMatch(path);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param taskName="taskName"></param>
        /// <returns></returns>
        public static bool IsValidDomainUserName(string name)
        {
            // LogLine(LogFile, "IsValidDomainUserName:\tStarting...");
            Regex validDomainUserRegEx = new Regex(@"^[^\]\\"" /\[:;|=,+*?<>]{1,15}\\[^\]\\""/\[:;|=,+*?<>]{1,20}(?<=[^\s])$");
            return validDomainUserRegEx.IsMatch(name);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param taskName="taskName"></param>
        /// <returns></returns>
        public static bool IsValidVariableName(string name)
        {
            // LogLine(LogFile, "IsValidVariableName:\tStarting...");
            Regex validDomainUserRegEx = new Regex(@"^(?=^.{1,255}$)([A-Za-z0-9][A-Za-z0-9_-]*)$");
            return validDomainUserRegEx.IsMatch(name);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="parameterName"></param>
        public static void ArgumentNotNull(object value, string parameterName)
        {
            if (value == null)
                throw new ArgumentNullException(parameterName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="parameterName"></param>
        public static void ArgumentNotNullOrEmpty(string value, string parameterName)
        {
            if (value == null)
                throw new ArgumentNullException(parameterName);

            if (string.IsNullOrEmpty(value))
                throw new ArgumentException(string.Format("'{0}' cannot be empty.", parameterName), parameterName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="parameterName"></param>
        public static void ArgumentGreaterThanZero(int value, string parameterName)
        {
            if (value < 1)
                throw new ArgumentException(string.Format("'{0}' must be greater than 0.", parameterName), parameterName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="parameterName"></param>
        public static void ArgumentNotNegative(int value, string parameterName)
        {
            if (value < 0)
                throw new ArgumentException(string.Format("'{0}' must be greater than or equal to zero.", parameterName), parameterName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="parameterName"></param>
        public static void ArgumentNodeListCountGreaterThanZero(XmlNodeList value, string parameterName)
        {
            if (value == null)
                throw new ArgumentNullException(parameterName);

            if (value.Count < 1)
                throw new ArgumentException(string.Format("'{0}' count must be greater than 0.", parameterName), parameterName);
        }
    }
}